package com.laowang.project.common.controller;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.laowang.common.constant.Constants;
import com.laowang.common.utils.IdUtils;
import com.laowang.common.utils.VerifyCodeUtils;
import com.laowang.common.utils.sign.Base64;
import com.laowang.framework.redis.RedisCache;
import com.laowang.framework.web.domain.AjaxResult;

/**
 * 验证码操作处理
 *
 * @author laowang
 */
@RestController
public class CaptchaController
{
    @Autowired
    private RedisCache redisCache;

    /**
     * 生成验证码
     */
    @GetMapping("/captchaImage")
    public AjaxResult getCode(HttpServletResponse response) throws IOException
    {
        // 生成随机字串
        String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
        // 唯一标识
        String uuid = IdUtils.simpleUUID();
        String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;

        redisCache.setCacheObject(verifyKey, verifyCode, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
        // 生成图片
        int w = 111, h = 36;
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        VerifyCodeUtils.outputImage(w, h, stream, verifyCode);
        try
        {
            AjaxResult ajax = AjaxResult.success();
            ajax.put("uuid", uuid);
            ajax.put("img", Base64.encode(stream.toByteArray()));
            return ajax;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        finally
        {
            stream.close();
        }
    }

    /*@GetMapping("/captchaImage")
    public AjaxResult getCode(HttpServletResponse response) throws IOException {

        Integer x =  randomNum();
        Integer y = randomNum();
        char z ;
        Integer m;
        Integer integer = randomNum1();

        String verifyCode ="";
        String verifyCode1 ="";
        if (integer == 0){
            //+
            z= '+';
            m = x +y;
            verifyCode1 = x +  " + " + y;
        }else if (integer == 1){
            //-
            z= '-';
            m = x - y;
            verifyCode1 = x +  " - " + y;
        }else {
            //*
            z = '-';
            m = x * y;
            verifyCode1 = x +  " * " + y;
        }
        verifyCode = String.valueOf(m);

        String uuid = IdUtils.simpleUUID();
        String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;

        redisCache.setCacheObject(verifyKey, verifyCode, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
        // 生成图片
        int w = 111, h = 36;
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        VerifyCodeUtils.outputImage(w, h, stream, verifyCode1);
        try
        {
            AjaxResult ajax = AjaxResult.success();
            ajax.put("uuid", uuid);
            ajax.put("img", Base64.encode(stream.toByteArray()));
            return ajax;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        finally
        {
            stream.close();
        }
    }
*/
    private Integer randomNum() {
        Integer x = (int) Math.round(Math.random()*(10)); //0 到255的随机数

        return x;
    }

    private    Integer randomNum1() {
        Integer x = (int) Math.round(Math.random()*(2)); //0 到2的随机数

        return x;
    }
}
